package com.nbsaas.boot.data.ext.handle;

import com.nbsaas.boot.data.api.apis.QueryTemplateApi;
import com.nbsaas.boot.data.api.domain.request.QueryTemplateRequest;
import com.nbsaas.boot.data.api.domain.response.QueryTemplateResponse;
import com.nbsaas.boot.data.ext.apis.HandleListApi;
import com.nbsaas.boot.data.ext.apis.ResultHandleWork;
import com.nbsaas.boot.data.ext.domain.data.HandleData;
import com.nbsaas.boot.data.utils.FreemarkerUtils;
import com.nbsaas.boot.rest.api.SqlExeApi;
import com.nbsaas.boot.rest.request.ResultHandleObject;
import com.nbsaas.boot.rest.request.ResultHandleParam;
import com.nbsaas.boot.rest.request.SqlObject;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.rest.response.MapResponse;
import com.nbsaas.boot.rest.response.ResponseObject;
import lombok.Setter;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Setter
@Component
public class ResultDbHandle implements ResultHandleWork {

    private QueryTemplateApi queryTemplateApi;


    private SqlExeApi sqlExeApi;


    private HandleListApi handleListApi;

    public ResultDbHandle() {
    }

    public void handle(ResultHandleObject handleObject, Map<String, Object> requestParam, MapResponse mapResponse) {
        String model = handleObject.getModel();
        if (!StringUtils.hasText(model)) {
            return;
        }
        QueryTemplateRequest req = new QueryTemplateRequest();
        req.setId(handleObject.getModel());
        ResponseObject<QueryTemplateResponse> queryTemplateResponseResponseObject = queryTemplateApi.view(req);
        if (queryTemplateResponseResponseObject.getCode() != 200) {
            return;
        }
        Map<String, Object> requestMap = new HashMap<>(requestParam);
        List<ResultHandleParam> params = handleObject.getParams();
        if (params != null) {
            for (ResultHandleParam param1 : params) {
                String name = param1.getParam();
                String type = param1.getType();
                String requestKey = param1.getRequestKey();
                if (!StringUtils.hasText(requestKey)) {
                    requestKey = name;
                }
                if ("result".equals(type)) {
                    //如果参数来源与结果
                    requestMap.put(requestKey, mapResponse.get(name));
                } else if ("value".equals(type)) {
                    requestMap.put(requestKey, name);
                } else {
                    requestMap.put(requestKey, requestParam.get(name));
                }
            }
        }
        SqlObject sqlObject = new SqlObject();
        sqlObject.setSql(FreemarkerUtils.process(queryTemplateResponseResponseObject.getData().getContent(), requestMap));
        if ("single".equals(handleObject.getResultType())) {
            ResponseObject<MapResponse> res = sqlExeApi.selectOne(sqlObject);
            if (res.getCode() != 200) {
                return;
            }
            MapResponse mapRes = res.getData();
            if (mapRes != null) {
                mapResponse.putAll(mapRes);
            }


            if (handleListApi != null) {

                //处理是否还有查询
                //处理查询
                ListResponse<MapResponse> listData = new ListResponse<>();
                listData.setData(new ArrayList<>());
                listData.getData().add(mapRes);

                ResponseObject<HandleData> temp = new ResponseObject<>();
                HandleData data = new HandleData();
                data.setResponse(queryTemplateResponseResponseObject.getData());
                data.setParam(new HashMap<>());
                temp.setData(data);
                handleListApi.handleList(temp, listData);
            }

        } else if ("list".equals(handleObject.getResultType())) {
            ListResponse<MapResponse> res = sqlExeApi.list(sqlObject);
            String resultKey = handleObject.getResultKey();
            if (!StringUtils.hasText(resultKey)) {
                resultKey = "items";
            }
            mapResponse.put(resultKey, res.getData());


            //处理是否还有查询
            if (handleListApi != null) {
                ResponseObject<HandleData> temp = new ResponseObject<>();
                HandleData data = new HandleData();
                data.setParam(new HashMap<>());
                data.setResponse(queryTemplateResponseResponseObject.getData());
                temp.setData(data);
                handleListApi.handleList(temp, res);
            }


        }
    }
}
